package com.edu.d10_test;
import java.util.*;

public class Test2 {
    //目标：红包雨游戏
    public static void main(String[] args) throws Exception {
        //1、拿红包准备好
        List<Integer> moneys = getMoneys();
        System.out.println(moneys);

        List<PeopleThread2> peopleThread2List=new ArrayList<>();
        //2、生成100个人
        for (int i = 0; i < 100; i++) {
            //也就是说，将红包交给100个线程对象，然后让下面100个线程去抢，也就是说，100个人共享这些红包雨
            PeopleThread2 peopleThread2 = new PeopleThread2(moneys, i + "号");
            peopleThread2.start();
            peopleThread2List.add(peopleThread2);
        }

        System.out.println("-------------");
        for (PeopleThread2 peopleThread2 : peopleThread2List) {
            //让每个线程都结束！
            peopleThread2.join();
        }
        System.out.println("活动结束！！！");
        //统计谁抢到的最多，降序排序
        peopleThread2List.stream().sorted(new Comparator<PeopleThread2>() {
            @Override
            public int compare(PeopleThread2 o1, PeopleThread2 o2) {
                return o2.getTotalMoney()- o1.getTotalMoney();
            }
        }).forEach(e-> System.out.println(e.getName()+"抢红包总计："+e.getTotalMoney()));
    }
    /**
     * 准备红包
     */
    public static List<Integer> getMoneys(){
        //生成红包，权重，百分之八十是小红包，百分之二十是大红包，总体200个红包
        Random random = new Random();
        List<Integer> list = new ArrayList<>();
        //生成小红包
        for (int i = 0; i < 160; i++) {
            int i1 = random.nextInt(30) + 1;
            list.add(i1);
        }
        //生成大红包
        for (int i = 0; i < 40; i++) {
            int i1 = random.nextInt(31) + 1;
            list.add(i1);
        }
        //将红包霍霍一起，打乱他
        Collections.shuffle(list);
        return list;
    }
}
